/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package apteka.admin;

import apteka.CategoryManager;
import apteka.ProductManager;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

/**
 *
 * @author Marek
 */

@Controller
@RequestMapping("/admin_panel/products.htm")
public class AdminProductsController {

    @Autowired
        CategoryManager cat_man;
    @Autowired
        ProductManager prod_man;

     @RequestMapping(method = RequestMethod.GET)
     public String showPage(Map model) {
         model.put("productList", prod_man.getProductList("", 0, 0));
         model.put("categoryList", cat_man.getMainCategories());
         model.put("pageCount",prod_man.getProductPagesCount("", 0));
         productFilter filter = new productFilter();
         filter.setMainCategory(0);
         filter.setSubCategory(0);
         model.put("productFilter",filter);
         model.put("page", "products.jsp");
         return "admin_panel/main";
     }

     @RequestMapping(params="op=showPage", method = RequestMethod.GET)
     public String showPage(@RequestParam("page") int page, 
             @RequestParam("main") int main,
             @RequestParam("sub") int sub,
             @RequestParam("string") String name,
             @RequestParam("items") int items,Map model) {
         productFilter filter = new productFilter();
         filter.setMainCategory(main);
         filter.setSubCategory(sub);
         filter.setItemsOnPage(items);
         filter.setProductName(name);
         filter.setPage(page);
         model.put("productFilter",filter);

         prod_man.setItemsOnPage(filter.getItemsOnPage());
         if (filter.getSubCategory()==0) {
            model.put("productList", prod_man.getProductList(filter.getProductName(), filter.getMainCategory(), filter.getPage()-1));
            model.put("pageCount",prod_man.getProductPagesCount(filter.getProductName(), filter.getMainCategory()));
         }
         else {
            model.put("productList", prod_man.getProductList(filter.getProductName(), filter.getSubCategory(), filter.getPage()-1));
            model.put("pageCount",prod_man.getProductPagesCount(filter.getProductName(), filter.getSubCategory()));
         }
         model.put("categoryList", cat_man.getMainCategories());
         model.put("subCategoryList", cat_man.getSubCategories(filter.getMainCategory()));

         model.put("page", "products.jsp");
         return "admin_panel/main";
     }

     @RequestMapping(method = RequestMethod.POST)
     public String processPage(productFilter filter, Map model) {
         prod_man.setItemsOnPage(filter.getItemsOnPage());
         if (filter.getSubCategory()==0) {
            model.put("productList", prod_man.getProductList(filter.getProductName(), filter.getMainCategory(), 0));
            model.put("pageCount",prod_man.getProductPagesCount(filter.getProductName(), filter.getMainCategory()));
         }
         else {
            model.put("productList", prod_man.getProductList(filter.getProductName(), filter.getSubCategory(), 0));
            model.put("pageCount",prod_man.getProductPagesCount(filter.getProductName(), filter.getSubCategory()));
         }
         model.put("categoryList", cat_man.getMainCategories());
         model.put("subCategoryList", cat_man.getSubCategories(filter.getMainCategory()));
         model.put("productFilter",filter);
         model.put("page", "products.jsp");
         return "admin_panel/main";
     }

     @RequestMapping(params="op=delete",method = RequestMethod.GET)
     public String deleteProduct(@RequestParam("id") int id, Map model) {
         prod_man.deleteProduct(id);
         model.put("productList", prod_man.getProductList("", 0, 0));
         model.put("categoryList", cat_man.getMainCategories());
         model.put("pageCount",prod_man.getProductPagesCount("", 0));
         productFilter filter = new productFilter();
         filter.setMainCategory(0);
         filter.setSubCategory(0);
         model.put("productFilter",filter);
         model.put("page", "products.jsp");
         return "admin_panel/main";
     }

}
